import 'package:flutter/material.dart';
import 'package:flutter_sound/public/flutter_sound_player.dart';
import 'package:flutter_svg/svg.dart';
import 'package:interview/components/app/app_ripple_button.dart';
import 'package:interview/config/app.enum.dart';
import 'package:interview/theme/app_colors.dart';

class RecordPlayer extends StatelessWidget {
  final AppColors theme;
  final bool isPlaying;
  final Duration currentPosition;
  final Duration totalDuration;
  final FlutterSoundPlayer audioPlayer;
  final String playPath;
  final void Function(Duration value) updateCurrentPosition;
  final void Function(String path) togglePlayPause;
  const RecordPlayer({
    super.key, 
    required this.isPlaying,
    required this.theme,
    required this.currentPosition, 
    required this.totalDuration, 
    required this.audioPlayer, 
    required this.playPath,
    required this.updateCurrentPosition,
    required this.togglePlayPause
  });


  String _formatDuration(Duration duration) {
    String twoDigits(int n) => n.toString().padLeft(2, "0");
    final minutes = twoDigits(duration.inMinutes.remainder(60));
    final seconds = twoDigits(duration.inSeconds.remainder(60));
    return "$minutes:$seconds";
  }


  void _seekTo(double milliseconds) async {
    final newPosition = Duration(milliseconds: milliseconds.toInt());
    await audioPlayer.seekToPlayer(newPosition); // 调用音频播放器的方法
    updateCurrentPosition(newPosition);
  }

  @override
  Widget build(BuildContext context) {
    return  Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(_formatDuration(currentPosition)), // 当前播放时间
              AppRippleButton(
                onTap:() {
                  togglePlayPause(playPath);
                },
                padding: const EdgeInsets.all(2),
                backgroundColor: Colors.transparent,
                borderRadius: BorderRadius.circular(2),
                splashColor:theme.subjectMix2!.withOpacity(0.3),
                highlightColor:theme.subjectMix2!.withOpacity(0.3) ,
                child: SvgPicture.asset(
                  isPlaying ? "${AppEnumAssets.basePath}pause.svg" : "${AppEnumAssets.basePath}play.svg",
                  width: 32,
                  height: 32,
                  colorFilter: ColorFilter.mode(theme.contrastPure!, BlendMode.srcIn),
                )
              ),
              Text(_formatDuration(totalDuration)),   // 总时长
            ],
          ),
          TweenAnimationBuilder<double>(
            tween: Tween<double>(
            begin: 0.0,
            end: currentPosition.inMilliseconds.toDouble(),
            ),
            duration: const Duration(milliseconds: 200), // 动画时长
            builder: (context, value, child) {
              return SliderTheme(
                data:  SliderTheme.of(context).copyWith(
                trackHeight: 1.0, 
                thumbShape: RoundSliderThumbShape(
                  enabledThumbRadius: 4.0, // 拖动块的半径
                  pressedElevation:1.0, // 拖动块按下时的阴影
                ),
              ), 
                child: Slider(
                  activeColor:theme.contrastPure,
                  inactiveColor:theme.subjectMix2,
                  thumbColor:theme.contrastPure,
                  value: value.clamp(0.0, totalDuration.inMilliseconds.toDouble()), // 限制范围 动态更新的值
                  onChanged: (newValue) => _seekTo(newValue), // 拖动改变播放进度
                  min: 0.0,
                  max: totalDuration.inMilliseconds.toDouble(),
                )
              );
            },
          ),
        ],
      ),
    );
  }
}